<template>
	<view class="main-content">
				<!-- <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white" height="206">
			<van-swipe-item v-for="(item,index) in state.config.banner" :key="index">
				<image class="wsipe-img" src="/static/img/banner.png" mode="widthFix"></image>
			</van-swipe-item>
			<image class="water-bgc" src="/static/img/waterBackground.png" mode="widthFix"></image>
		</van-swipe> -->
				<!-- <MHeader v-if="state.myInfo.role.includes(7)">基础资料</MHeader>
		<van-grid :border="false">
			<van-grid-item :icon="item.Logo" :text="item.MenuName" v-for="(item,index) in state.menu.filter(it=>it.role.some(iter=>state.myInfo.role.includes(iter)))" :key="index" @click.stop="toPage(item,0)" />
		</van-grid> -->
		<view class="cards-" v-if="state.myInfo.role.includes(3)||state.myInfo.role.includes(7)">
			<view class="nums">
				<view class="line"> </view>
				<view class="nums-it">
					<view class="txt">本月上苗</view>
					<view class="num">{{state.F_QUQY_SMQTY || 0}}</view>
				</view>
				<view class="nums-it">
					<view class="txt">本月回收</view>
					<view class="num">{{state.F_QUQY_HSQTY || 0}}</view>
				</view>
			</view>
		</view>

		<view class="gap"></view>
		<MHeader>单据列表</MHeader>
		<van-grid :border="false">
			<van-grid-item :icon="item.Logo" :text="item.MenuName" v-for="(item,index) in state.menu2.filter(it=>it.role.some(iter=>state.myInfo.role.includes(iter)))" :key="index" @click.stop="toPage(item,1)" />
		</van-grid>
	</view>
</template>

<script setup>
	import moment from 'moment'
	import { reactive, getCurrentInstance, ref, onBeforeMount } from 'vue'
	import { onLoad, onShow } from '@dcloudio/uni-app'
	import kdApis from '/api/modularApi/kdApi.js'
	const { proxy } = getCurrentInstance()
	const state = reactive({
		myInfo: {},
		config: proxy.$config,
		menu: [{
				MenuName: "物料列表",
				Logo: "/static/img/i001.png",
				WapUrl: "/pages/baseList/BaseList",
				FormId: "BD_MATERIAL",
				role: [7, 11]
			},
			{
				MenuName: "供应商列表",
				Logo: "/static/img/i002.png",
				WapUrl: "/pages/baseList/BaseList",
				FormId: "BD_Supplier",
				role: [7, 11]
			},
			{
				MenuName: "计量单位",
				Logo: "/static/img/i003.png",
				WapUrl: "/pages/baseList/BaseList",
				FormId: "BD_UNIT",
				role: [7, 11]
			},
			// {
			// 	MenuName: "养户列表",
			// 	Logo: "/static/img/i004.png",
			// 	WapUrl: "/pages/baseList/BaseList",
			// 	FormId: ""
			// },
			{
				MenuName: "客户列表",
				Logo: "/static/img/i005.png",
				WapUrl: "/pages/baseList/BaseList",
				FormId: "BD_Customer",
				role: [7, 11]
			},
			{
				MenuName: "司机列表",
				Logo: "/static/img/i006.png",
				WapUrl: "/pages/baseList/BaseList",
				FormId: "BAS_PreBaseDataTwo",
				role: [7, 11]
			},
			{
				MenuName: "员工列表",
				Logo: "/static/img/i007.png",
				WapUrl: "/pages/baseList/BaseList",
				FormId: "BD_Empinfo",
				role: [7, 11]
			}
		],
		menu2: [{
				MenuName: "上苗计划",
				Logo: "/static/img/i008.png",
				WapUrl: "/pages/baseList/BaseList",
				FormId: "QUQY_SeedPlan",
				IsAE: 1,
				role: [3, 7, 11]
			},
			{
				MenuName: "苗损列表",
				Logo: "/static/img/i009.png",
				WapUrl: "/pages/baseList/BaseList",
				FormId: "QUQY_LossInfoMiao",
				IsAE: 1,
				role: [3, 5, 7, 11]
			},
			{
				MenuName: "养殖合同",
				Logo: "/static/img/i010.png",
				WapUrl: "/pages/baseList/BaseList",
				FormId: "QUQY_BreContract",
				IsAE: 0,
				role: [7, 11]
			},
			{
				MenuName: "养殖合同明细",
				Logo: "/static/img/i010.png",
				WapUrl: "/pages/baseList/BaseList",
				FormId: "QUQY_GetBreContract",
				IsAE: 0,
				role: [3, 7, 11]
			},
			{
				MenuName: "饲料申请",
				Logo: "/static/img/i011.png",
				WapUrl: "/pages/baseList/BaseList",
				FormId: "QUQY_FeedApplication",
				IsAE: 1,
				role: [1, 7, 11]
			},
			{
				MenuName: "饲料出库",
				Logo: "/static/img/i016.png",
				WapUrl: "/pages/baseList/BaseList",
				FormId: "QUQY_SalesDeliveryOrder",
				IsAE: 0,
				role: [1, 7, 11]
			},
			// {
			// 	MenuName: "兽药申请",
			// 	Logo: "/static/img/i012.png",
			// 	WapUrl: "/pages/baseList/BaseList",
			// 	FormId: "QUQY_VeterinaryMedicineApplication", // 开药
			// 	IsAE: 1,
			// 	role: [5, 7]
			// },
			{
				MenuName: "兽药出库",
				Logo: "/static/img/i014.png",
				WapUrl: "/pages/baseList/BaseList",
				FormId: "QUQY_DrugOutbound",
				IsAE: 1,
				role: [5, 7, 11]
			},
			// {
			// 	MenuName: "调拨申请",
			// 	Logo: "/static/img/i015.png",
			// 	WapUrl: "/pages/baseList/BaseList",
			// 	FormId: "QUQY_TransferApplication",
			// 	IsAE: 1,
			// 	role: [5, 7]
			// },
			{
				MenuName: "兽药调拨",
				Logo: "/static/img/i015.png",
				WapUrl: "/pages/baseList/BaseList",
				FormId: "QUQY_VeterinaryDrugAllot",
				IsAE: 1,
				role: [5, 7, 11]
			},
			{
				MenuName: "库存查询",
				Logo: "/static/img/i012.png",
				WapUrl: "/pages/baseList/BaseList",
				FormId: "QUQY_VDInventoryTable",
				IsAE: 0,
				role: [5, 7, 11]
			},
			{
				MenuName: "养殖结算单",
				Logo: "/static/img/i013.png",
				WapUrl: "/pages/baseList/BaseList",
				FormId: "QUQY_Settle",
				IsAE: 0,
				role: [3, 7, 11]
			},
			{
				MenuName: "养户结算单",
				Logo: "/static/img/i013.png",
				WapUrl: "/pages/baseList/BaseList",
				FormId: "QUQY_YHSettle",
				IsAE: 0,
				role: [3, 7, 11],
			}
		],
		F_QUQY_HSQTY: 0,
		F_QUQY_SMQTY: 0

	})

	onShow(() => {
		state.myInfo = uni.getStorageSync('myInfo')
		getCard()
	})
	

	const toPage = (item) => {
		// 🍕 当我的角色有 财务 时
		let roleN7 = item.role.filter(it => it !== 7)
		if (roleN7[0] && item.IsAE == 1 && !state.myInfo.role.includes(roleN7[0])) item.IsAE = 0
		// 苗损：
		if(item.FormId == 'QUQY_LossInfoMiao' && state.myInfo.role.includes(5)) item.IsAE = 1
		uni.navigateTo({
			url: item.WapUrl + `${item.WapUrl.includes('?')?'&':'?'}` + `FormId=${item.FormId}&Name=${item.MenuName}&IsAE=${item.IsAE && Number(item.IsAE)}`
		})
	}

	const getCard = () => {
			let memberInfo = uni.getStorageSync('memberInfo')
			// 淮北安鑫业务员隐藏养殖结算单
			if( memberInfo ){
				if(memberInfo?.F_QUQY_COMPANY.Id === 10079 || memberInfo?.F_QUQY_COMPANY.Number === '303') state.menu2 = state.menu2.filter(it => it.FormId !== "QUQY_Settle")
				// console.log(memberInfo.F_QUQY_COMPANY.Id)
				if(memberInfo?.F_QUQY_COMPANY.Id !== 10079 && memberInfo?.F_QUQY_COMPANY.Number !== '303') state.menu2 = state.menu2.filter(it => it.FormId !== "QUQY_YHSettle")
			}
			
			// 养殖户结算单只给 淮北安鑫的 业务员、公司管理员、养殖场会计看
		if (state.myInfo.role.includes(3) || state.myInfo.role.includes(7)) {
			let time = new Date()
			let year = time.getFullYear()
			let month = time.getMonth() + 1
			let FilterString = [
				{ "Left": "(", "FieldName": "F_QUQY_YEAR", "Compare": "=", "Value": year, "Right": ")", "Logic": "AND" },
				{ "Left": "(", "FieldName": "F_QUQY_MONTH", "Compare": "=", "Value": month, "Right": ")", "Logic": "AND" },
				{ "Left": "(", "FieldName": "F_QUQY_ORGID", "Compare": "=", "Value": memberInfo.F_QUQY_COMPANY_Id, "Right": ")", "Logic": "AND" },
			]
			if(state.myInfo.role.includes(3)){
				FilterString.push({ "Left": "(", "FieldName": "F_QUQY_YWY", "Compare": "=", "Value": memberInfo.Id, "Right": ")", "Logic": "AND" })
			}
			kdApis.formQuery({
				data: {
					FormId: 'QUQY_YWYCOUNT',
					FieldKeys: "F_QUQY_SMQTY,F_QUQY_HSQTY",
					FilterString
				}
			}).then(res => {
				if (res) {
					state.F_QUQY_HSQTY = res.reduce((preV,next)=>preV + next.F_QUQY_HSQTY,0)
					state.F_QUQY_SMQTY = res.reduce((preV,next)=>preV + next.F_QUQY_SMQTY,0)
				}
			})
		}

	}
</script>
<style lang="scss" scoped>
	.main-content {
		height: calc(100% - 120rpx);

		.my-swipe {
			position: relative;

			.wsipe-img {
				width: 100%;
			}
		}

		::v-deep .van-icon__image {
			width: 96rpx;
			height: 96rpx;
		}

		::v-deep .van-grid-item__text {
			font-size: 24rpx;
			white-space: nowrap;
		}

		.menu-page {
			position: relative;
			// width: 60rpx;
			height: 6rpx;
			background: #d9d9d9;
			border-radius: 3rpx;
			margin: 0 auto 30rpx;
			overflow: hidden;

			.menu-page-inner {
				position: absolute;
				width: 30rpx;
				height: 6rpx;
				background: #409eff;
				transition: all 0.4s;
			}
		}

		.cards- {
			width: 100%;
			background: #f7f7f7;
			padding: 30rpx 30rpx 20rpx 30rpx;

			.nums {
				position: relative;
				display: flex;
				width: 100%;
				height: 200rpx;
				background: #fff;
				border-radius: 10rpx;
				overflow: hidden;

				.line {
					position: absolute;
					width: 100%;
					height: 10rpx;
					background: #0ca333;
				}

				.nums-it {
					display: flex;
					flex-direction: column;
					align-items: center;
					justify-content: center;
					width: 50%;
					height: 100%;

					.txt {
						line-height: 80rpx;
						color: #a6a6a6;
					}

					.num {
						font-size: 40rpx;
						font-weight: 800;
					}
				}
			}
		}
	}
</style>